﻿using Base.Infrastructure.Dto.Request.Classes;
using Base.Infrastructure.Dto.Response;
using Base.Infrastructure.EFCore.MySql;
using Base.Infrastructure.IOC;
using Base.Infrastructure.Tools;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base.Service.Instance
{
    public class ClassesService : BaseService, Base.Service.Interface.IClassesService
    {
        private readonly ClassesIOC _classesIOC;

        public ClassesService(ClassesIOC classesIOC, IConfiguration configuration)
        {
            _classesIOC = classesIOC;
        }

        /// <summary>
        /// 添加班级
        /// </summary>
        /// <returns></returns>
        private bool AddClass(Class @class)
        {
            _classesIOC._classesService.Add(@class);
            return _classesIOC._classesService.SaveChanges() > 0;
        }

        /// <summary>
        /// 添加班级
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult Addclass(CLassesRequestDto dto)
        {
            string id = Config.GUID();
            this.AddClass(new Class
            {
                Cid = id,
                Cname= dto.CName,
                Ctype=dto.CType,
                Ccount=dto.CCount,
                CisFinish=dto.CIsFinish,
                CisActive=dto.CIsActive,
                CcreateTime= DateTime.Now.ToString()
            }) ;
            return GetResult(1, "操作成功");
        }

        /// <summary>
        /// 查询班级表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult Queryclass(int page,int limit,string key)
        {
            var ai = _classesIOC._classesService.QueryAll(out int total, page, limit, d => d.CcreateTime, false, d => d.Cname.Contains(key));

            var dto1 =ai.Select(d=>new
            {
                CID = d.Cid,
                CName= d.Cname,
                CTypeAID= d.Ctype,
                CCount= d.Ccount,
                CIsFinish= d.CisFinish,
                CIsActive= d.CisActive,
                CCreateTime= d.CcreateTime
            }).ToList();
            return GetResult(1, data: dto1);
        }

        /// <summary>
        /// 单个查询班级
        /// </summary>
        /// <param name="CID"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult Singlequeryclass(string CID)
        {
            var ai = _classesIOC._classesService.QueryAll(d => d.Cid==CID);
            if (ai.Any())
            {
                var ia = ai.Single();
                CLassesRequestDto dto1 = new CLassesRequestDto
                {
                    CID = ia.Cid,
                    CName = ia.Cname,
                    CType = ia.Ctype,
                    CCount = ia.Ccount,
                    CIsFinish = ia.CisFinish,
                    CIsActive = ia.CisActive,
                    CCreateTime = ia.CcreateTime
                };
                return GetResult(1, data: dto1);
            }
            else
            {
                return GetResult(-1,"没有这个班级");
            }
        }

        /// <summary>
        /// 修改班级
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult Modifyclass(CLassesRequestDto dto)
        {
            var ai = _classesIOC._classesService.QueryAll(d => d.Cid == dto.CID);
            if (ai.Any())
            {
                var ia = ai.Single();
                ia.Cname = dto.CName;
                ia.Ctype = dto.CType;
                ia.Ccount = dto.CCount;
                ia.CisFinish = dto.CIsFinish;
                ia.CisActive = dto.CIsActive;
                _classesIOC._classesService.Update(ia);
                _classesIOC._classesService.SaveChanges();
                return GetResult(1, "操作成功");
            }
            else
            {
                return GetResult(-1, "找不到此班级");
            }
        }
    }
}
